package com.zht.utils;

import org.apache.commons.lang3.StringUtils;

import java.util.Base64;

/**
 * 十六进制图片数据转base64
 *
 */
public class PicUtils {


    public static String encode(byte[] data) {
        return Base64.getEncoder().encodeToString(data);
    }

    public static byte[] hexStringToByteArray(String s) {
        int len = s.length();
        byte[] data = new byte[len / 2];
        for (int i = 0; i < len; i += 2) {
            data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
                    + Character.digit(s.charAt(i + 1), 16));
        }
        return data;
    }

    public static String hexadecimalToBase64(String content){
        if(StringUtils.isNotBlank(content)){
            return encode(hexStringToByteArray(content));
        }else {
            return content;
        }

    }
}
